﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using PVAutoTest.TestMngr;
using MoonLight.LibCommon;

namespace PVAutoTest
{
    public partial class CreateTestSuitUi : Form
    {
        public CreateTestSuitUi()
        {
            InitializeComponent();
        }

        public void SetTestSuitCases(DataSet ds, int time_step, PVAutoTestUi parent)
        {
            m_e = new CreateTestSuitEventArgs();
            m_e.SetTestSuit(ds, time_step, parent);
        }

        private void button_Browse_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK) 
            {
                if (folderBrowserDialog1.SelectedPath != "")
                    textBox2.Text = folderBrowserDialog1.SelectedPath;
            }
        }

        private void button_Save_Click(object sender, EventArgs e)
        {
            string ts_name = textBox1.Text;
            string ts_dir = textBox2.Text;
            if (ts_name == "" || ts_dir == "")
            {
                MessageBox.Show("Please enter TestSuit Name and Store Path");
                return;
            }
            string ts_path = Path.Combine(ts_dir, ts_name);
            if (Directory.Exists(ts_path)) {
                if (MessageBox.Show("TestSuit is already exist, cover it?", "Create TestSuit Folder", MessageBoxButtons.OKCancel) != DialogResult.OK)
                {
                    return;
                }
                else {
                    try
                    {
                        string[] file_list = Directory.GetFiles(ts_path, "*", SearchOption.AllDirectories);
                        for (int i = 0; i < file_list.Length; i++)
                        {
                            File.Delete(file_list[i]);
                        }
                        Directory.Delete(ts_path, true);
                    }
                    catch (Exception ex) {
                        Logger.WriteLogError(ex.Message);
                        MessageBox.Show("Cannot delete directory[" + ts_path + "]");
                    }
                }
            }

            try
            {
                DirectoryInfo di = new DirectoryInfo(ts_path);
                di.Create();

                string ts_data_path = Path.Combine(ts_path, "TestData");
                DirectoryInfo di_ts_data = new DirectoryInfo(ts_data_path);
                di_ts_data.Create();

                string ts_result_path = Path.Combine(ts_path, "TestResult");
                DirectoryInfo di_ts_result = new DirectoryInfo(ts_result_path);
                di_ts_result.Create();

                m_e.SetTestSuitPath(ts_path, ts_data_path, ts_result_path);
                if (OnChange != null)
                    OnChange(sender, m_e);
            }
            catch (Exception ex) {
                Logger.WriteLogError(ex.Message);
                MessageBox.Show("Create testsuit fail");
            }
            this.Close();
        }

        private void button_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        public delegate void TestSuitCreated(object sender, CreateTestSuitEventArgs e);
        public event TestSuitCreated OnChange;
        private CreateTestSuitEventArgs m_e;
    }

    public class CreateTestSuitEventArgs : EventArgs
    {
        public CreateTestSuitEventArgs()
        {
            m_ts = new TestSuit();
        }

        public void SetTestSuit(DataSet ds, int time_step, PVAutoTestUi parent)
        {
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++) 
            {
                int times = Convert.ToInt32(ds.Tables[0].Rows[i]["Times"].ToString());
                for (int j = 0; j < times; j++)
                {
                    TestCase tc = new TestCase(m_ts);
                    TestData_Event td_e = new TestData_Event();
                    td_e.case_no = Convert.ToInt32(ds.Tables[0].Rows[i]["CaseNo"].ToString());
                    td_e.case_sub_no = j + 1;
                    td_e.msg_no = ds.Tables[0].Rows[i]["MessageNo"].ToString();
                    td_e.dev_id = ds.Tables[0].Rows[i]["DeviceId"].ToString();
                    td_e.set_state = ds.Tables[0].Rows[i]["SetState"].ToString();
                    td_e.component_id = ds.Tables[0].Rows[i]["ComponentId"].ToString();
                    td_e.component_state = ds.Tables[0].Rows[i]["ComponentState"].ToString();
                    td_e.event_no = ds.Tables[0].Rows[i]["EventNo"].ToString();
                    td_e.msg_str = ds.Tables[0].Rows[i]["MessageString"].ToString();
                    td_e.event_id += i * times + j;
                    tc.Init(td_e, 0);
                    tc.OnChange += new TestCase.Delegate_TestCaseResult(parent.TestCase_OnChange);
                    m_ts.AddTestCase(tc);
                }
            }
            m_ts.SetTimeStep(time_step);
        }

        public void SetTestSuitPath(string path, string data_path, string result_path)
        {
            m_ts.SetSuitPath(path, data_path, result_path);
        }

        public TestSuit GetTestSuit()
        {
            return m_ts;
        }

        private TestSuit m_ts;
    }

}
